const Koa = require('koa')
const app = new Koa()
const views = require('koa-views')
const json = require('koa-json')
const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const { logger, accessLogger } = require('./libs/logger')
const routes = require('./routes/index')

// error handler
onerror(app)

// middlewares
app.use(accessLogger())
app.use(bodyparser({
    enableTypes: ['json', 'form', 'text']
}))
app.use(json())
app.use(require('koa-static')(__dirname + '/public'))

// 加载模板引擎
app.use(views(__dirname + '/views', {
    extension: 'pug'
}))



// routes
routes(app)

app.on('error', (err, ctx) => {
    logger.error('server error', err, ctx)
});


module.exports = app
